import { CALL_API } from 'redux-api-middleware'
import { extend } from 'lodash/fp'
import config from 'config'

// Constants
export const FETCH_ARTICLE_REQUEST = 'FETCH_ARTICLE_REQUEST'
export const FETCH_ARTICLE_SUCCESS = 'FETCH_ARTICLE_SUCCESS'
export const FETCH_ARTICLE_FAILURE = 'FETCH_ARTICLE_FAILURE'

export const FETCH_ARTICLE_REPLIES_REQUEST = 'FETCH_ARTICLE_REPLIES_REQUEST'
export const FETCH_ARTICLE_REPLIES_SUCCESS = 'FETCH_ARTICLE_REPLIES_SUCCESS'
export const FETCH_ARTICLE_REPLIES_FAILURE = 'FETCH_ARTICLE_REPLIES_FAILURE'

export const ARTICLECOMMENT_REQUEST = 'ARTICLECOMMENT_REQUEST'
export const ARTICLECOMMENT_SUCCESS = 'ARTICLECOMMENT_SUCCESS'
export const ARTICLECOMMENT_FAILURE = 'ARTICLECOMMENT_FAILURE'

// 论坛文章内容
export function fetchArticleDetailed (id = '1') {
  return {
    [CALL_API]: {
      endpoint: config.api_root_v1 + '/article.php?c=article',
      method: 'POST',
      body: JSON.stringify({
        "jsonrpc": "2.0",
        "method": "getArticleContent",
        "params": [{
          "content_id": id
        }],
        "id": 1
      }),
      types: [
        FETCH_ARTICLE_REQUEST,
        {
          type: FETCH_ARTICLE_SUCCESS,
          payload: (action, state, res) => {
            return res.json()
          }
        },
        FETCH_ARTICLE_FAILURE
      ]
    }
  }
}

// 获取文章评论
export function fetchArticleReplies (id = '') {
  return {
    [CALL_API]: {
      endpoint: config.api_root_v1 + '/article.php?c=article',
      method: 'POST',
      body: JSON.stringify({
        "jsonrpc": "2.0",
        "method": "getContentComment",
        "params": [{
          "content_id": id
        }],
        "id": 1
      }),
      types: [
        FETCH_ARTICLE_REPLIES_REQUEST,
        {
          type: FETCH_ARTICLE_REPLIES_SUCCESS,
          payload: (action, state, res) => {
            return res.json()
          }
        },
        FETCH_ARTICLE_REPLIES_FAILURE
      ]
    }
  }
}

// 发布评论
export function articleComment (content_id = '', uid = '', comment_content = '') {
  return {
    [CALL_API]: {
      endpoint: config.api_root_v1 + '/article.php?c=article',
      method: 'POST',
      body: JSON.stringify({
        "jsonrpc": "2.0",
        "method": "articleContentComment",
        "params": [{
          "content_id": content_id,
          "u_id": uid,
          "comment_content": comment_content
        }],
        "id": 1
      }),
      types: [
        ARTICLECOMMENT_REQUEST,
        {
          type: ARTICLECOMMENT_SUCCESS,
          payload: (action, state, res) => {
            return res.json()
          }
        },
        ARTICLECOMMENT_FAILURE
      ]
    }
  }
}

export const actions = {
  fetchArticleDetailed,
  fetchArticleReplies,
  articleComment

}

// Action Handlers
const ACTION_HANDLERS = {
  [FETCH_ARTICLE_REQUEST]: (state, action) => {
    return ({ ...state, isFetching: true, error: {}, article: {} })
  },
  [FETCH_ARTICLE_SUCCESS]: (state, action) => {
    return ({ ...state, isFetching: false, lastUpdate: Date.now(), article: action.payload })
  },
  [FETCH_ARTICLE_FAILURE]: (state, action) => {
    return ({ ...state, isFetching: false, error: {}, article: {} })
  },
  [FETCH_ARTICLE_REPLIES_REQUEST]: (state, action) => {
    return ({ ...state, repliesItem: { isFetching: true, error: {}, replies: {} } })
  },
  [FETCH_ARTICLE_REPLIES_SUCCESS]: (state, action) => {
    return ({ ...state, repliesItem: { isFetching: false, lastUpdate: Date.now(), replies: action.payload } })
  },
  [FETCH_ARTICLE_REPLIES_FAILURE]: (state, action) => {
    return ({ ...state, repliesItem: { isFetching: false, error: {}, replies: {} } })
  },
  [ARTICLECOMMENT_REQUEST]: (state, action) => {
    return ({ ...state, commentItem: { isFetching: true, error: {}, comment: {} } })
  },
  [ARTICLECOMMENT_SUCCESS]: (state, action) => {
    return ({ ...state, commentItem: { isFetching: false, lastUpdate: Date.now(), comment: action.payload } })
  },
  [ARTICLECOMMENT_FAILURE]: (state, action) => {
    return ({ ...state, commentItem: { isFetching: false, error: {}, comment: {} } })
  }
}

// Reducers
const initialState = { isFetching: false, error: {}, repliesItem: { isFetching: false, error: {}, replies: {} }, commentItem: { isFetching: false, error: {}, comment: {} }, article: {} }
export default function articleDetailedReducer (state = initialState, action) {
  const handler = ACTION_HANDLERS[action.type]
  return handler ? handler(state, action) : state
}
